﻿using CP.Utility;
using GPM;
using LF3.Controllers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LF3.Moduls;
using DevExpress.XtraGrid.Views.Grid;

namespace LF3Proj
{
    public partial class FrmHeatHistory : BaseForm
    {
        private string STNO = "";
        private string HeatID = "";
        private long PlanID = 0;
        public FrmHeatHistory()
        {
            InitializeComponent();
        }

        private void FrmHeatHistory_Load(object sender, EventArgs e)
        {
            this.dtStart.EditValue = DateTime.Now;
            this.dtEnd.EditValue = DateTime.Now;
            Lf_Heat_ReportController ctl = new Lf_Heat_ReportController();
            DataTable dt = ctl.GetHeatID();

            this.lkupHeatid.Properties.DataSource = dt;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if ((this.lkupHeatid.EditValue == null) && (this.dtStart.EditValue == null) && (this.dtEnd.EditValue == null))
            {
                ExMessageBox.Show("查询条件不能为空", AppSetter.SoftName);
                return;
            }

            if (this.lkupHeatid.EditValue == null)
            {
                if (this.dtStart.EditValue == null)
                {
                    ExMessageBox.Show("开始时间不能为空", AppSetter.SoftName);
                    return;
                }

                if (this.dtEnd.EditValue != null)
                {
                    if (Convert.ToDateTime(this.dtEnd.EditValue) < Convert.ToDateTime(this.dtStart.EditValue))
                    {
                        ExMessageBox.Show("结束时间必须大于等于开始时间", AppSetter.SoftName);
                        return;
                    }
                }
            }
            string heatid = "";
            string start = "";
            string end = "";

            if (this.lkupHeatid.EditValue != null)
                heatid = this.lkupHeatid.EditValue.ToString();
            if (this.dtStart.EditValue != null)
                start = Convert.ToDateTime(this.dtStart.EditValue).ToString("yyyy-MM-dd");
            if (this.dtEnd.EditValue != null)
                end = Convert.ToDateTime(this.dtEnd.EditValue).AddDays(1).ToString("yyyy-MM-dd");

            Lf_Heat_ReportController ctl = new Lf_Heat_ReportController();
            this.gcHeatReport.DataSource = ctl.GetHeatReportCondition(heatid, start, end);
        }

        private void gvHeatReport_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            Lf_Heat_Report heatrpt = gvHeatReport.GetRow(e.FocusedRowHandle) as Lf_Heat_Report;
            if (null == heatrpt)
            {
                heatrpt = new Lf_Heat_Report();
                heatrpt.heatid = "";
                this.STNO = "";
                this.HeatID = "";
                this.PlanID = 0;
            }
            else
            {
                this.STNO = heatrpt.plansteelgrade;
                this.HeatID = heatrpt.heatid;
                this.PlanID = heatrpt.bofschedleid;
            }
            Lf_Material_ConsumeController ctl = new Lf_Material_ConsumeController();
            this.gcMatConsume.DataSource = ctl.GetMatConsume(heatrpt.heatid);
            this.gcWs.DataSource = ctl.GetWSConsume(heatrpt.heatid);
            Lf_Measuring_InfoController mctl = new Lf_Measuring_InfoController();
            this.gcTemp.DataSource = mctl.GetMeasureInfo(heatrpt.heatid);
            Lf_Steel_Sample_AnalysisController sctl = new Lf_Steel_Sample_AnalysisController();
            this.gcLFSample.DataSource = sctl.GetLFSample(heatrpt.heatid);
        }

        private void gvLFSample_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            try
            {
                GridView View = sender as GridView;
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                DataTable dt = ctrl.GetHeatStandard(this.STNO);
                if (dt.Rows.Count < 1) return;

                if (e.Column.FieldName.Contains("_h") && e.Column.FieldName != "zn_h" && e.Column.FieldName != "bi_h"
                    && e.Column.FieldName != "bso1_h" && e.Column.FieldName != "re_h"
                    && e.Column.FieldName != "di_h" && e.Column.FieldName != "ceq_h"
                     && e.Column.FieldName != "mnq_h")
                {
                    string colname = e.Column.FieldName.Substring(0, e.Column.FieldName.IndexOf("_h"));
                    object oValue = View.GetRowCellValue(e.RowHandle, View.Columns[e.Column.FieldName]);
                    if (oValue != null)
                    {
                        double value = Convert.ToDouble(oValue);
                        double min = dt.Rows[0][colname + "_min"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_min"]);
                        double max = dt.Rows[0][colname + "_max"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_max"]);
                        if (value > max || value < min)
                        {
                            e.Appearance.BackColor = Color.Red;
                            // e.Appearance.BackColor2 = Color.LightCyan;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message);
            }
        }

        private void btnMaterail_Click(object sender, EventArgs e)
        {
            if (this.PlanID == 0)
            {
                ExMessageBox.Show("没有选中炉次信息，不能修改投料数据", AppSetter.SoftName);
                return;
            }
            FrmCurrentMaterialConsume form = new FrmCurrentMaterialConsume(this.HeatID, this.PlanID);
            form.ShowDialog();

            Lf_Material_ConsumeController ctl = new Lf_Material_ConsumeController();
            this.gcMatConsume.DataSource = ctl.GetMatConsume(this.HeatID);
            this.gcWs.DataSource = ctl.GetWSConsume(this.HeatID);
        }

        private void gvHeatReport_RowClick(object sender, RowClickEventArgs e)
        {
            
        }
    }
}
